package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import java.util.List;
import java.util.Map;

/**
 * Created with IDEA
 * author:zhiXiang-Chen
 * Date:2019/2/26
 * Time:15:59
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
       List<Label> labels =labelService.findAll();

        return new Result( StatusCode.OK,true,"查询成功",labels);
    }

    /**
     * 增加
     * @param label
     * @return
     */
    @RequestMapping( method = RequestMethod.POST)
    public Result add(@RequestBody Label label){
        labelService.add(label);

        return new Result(StatusCode.OK,true,"增加成功");
    }

    /**
     * 根据id查询
     * @param
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findByid(@PathVariable(value = "id") String labelid){
        Label list = labelService.findByid(labelid);

        return new Result(StatusCode.OK,true,"查询成功",list);
    }

    /**
     * 更新
     * @param
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String labelid,@RequestBody Label label){
        label.setId(labelid);
        labelService.update(label);

        return new Result(StatusCode.OK,true,"修改成功");
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String labelid){

        labelService.delete(labelid);

        return new Result(StatusCode.OK,true,"删除成功");
    }
    /**
     * 查询分页
     * @param
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public Result search(@RequestBody Map searchMap){
        List<Label> list = labelService.search(searchMap);

        return new Result(StatusCode.OK,true,"查询成功",list);
    }

    @RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
    public Result search(@PathVariable(value = "page") int page,@PathVariable(value = "size") int size,@RequestBody Map searchMap){
        Page<Label> list = labelService.search(searchMap,page,size);

        PageResult pageResult = new PageResult(list.getTotalElements(),list.getContent());
        return new Result(StatusCode.OK,true,"查询成功",pageResult);
    }

}
